// wode/zhiyuan/zhiyuan.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    state:[],
    flag:false, //状态state数组有没有第一次渲染
    
    startY: 0,     //滑动开始y轴位置
    lastY: 0,     //滑动结束y轴位置
  },

  //滑动事件触发的函数
  touchstart: function (e) {
    this.data.startY = e.changedTouches[0].clientY
  },
  touchmove: function(e) {
    this.data.lastY = e.changedTouches[0].clientY
  },
  touchend:function(e){
    if (this.data.lastY < this.data.startY) {
      wx.navigateTo({
        url: '../form/form',
      })
    }
  },

  shenqing:function(e){
    var userid =wx.getStorageSync('userid');
    var activityid = e.currentTarget.dataset.activityid
    var activityname = e.currentTarget.dataset.activityname
    var index = e.currentTarget.dataset.index
    console.log(userid)
    this.setData({
      ['state['+index+']']: "已申请",
    })
    wx.setStorageSync('states', this.data.state)
    console.log(wx.getStorageSync('states'))
    //发起post请求存入shenqing表中
    wx.request({
      url : "https://www.prebeauty.work:3001/api/shenqing",
      method: "POST",
      data: {
        userid: userid, //用户id
        activityid: activityid, //活动id
        activityname: activityname //活动名称
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res.data); 
        wx.showToast({
          title: '活动申请成功！',
          icon: 'success',
          duration: 2000
        })
        
    },
    
  })

  //发起post请求数据存入heartfoot表中
  var userid = wx.getStorageSync('userid');
  var util = require('../../pages/utils/util.js');
  var time = util.formatTime(new Date())
  wx.request({
    url : "https://www.prebeauty.work:3001/api/heart",
    method: "POST",
    data: {
      userid: userid,
      type: 3, //申请类型
      time: time,
      activityname: activityname
    },
    header: {
      "content-type": "application/json"
    },
    success: function (res) {
      console.log(res.data); 
    },
  })

},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      groupid: wx.getStorageSync('groupid')
    })
    if(this.data.flag){
      this.setData({
        state: wx.getStorageSync('states')
      })
    } 

    wx.request({
      url: 'https://www.prebeauty.work:3001/api/aspiration ',
      header:{
        'content-type':'application/json'
      },
      success : res => {
        res.data.forEach((item,index) =>{
          this.setData({
            ['state['+index+']']: "申请",
            flag:true
          }) 
        }) 
        this.setData({
          list:res.data
        })
        console.log(this.data.list)         
      }
    })
  },

 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res) {
          if (res.confirm) {
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home'
           })
          } else if (res.cancel) {
            console.log('取消')
            wx.switchTab({
              url: '/pages/index/index'
           })
          }
        }
      })
    }
     //文字逐个显示
     var that = this
     var story = "从这里向上滑动发布新的志愿活动";
     var i= 0;
     var time = setInterval(function(){
         var text = story.substring(0, i);
         i++;
         that.setData({
             text: text
         });
         if (text.length == story.length) {
           //   console.log("定时器结束！");
             clearInterval(time);
         }
     },200)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})